package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.presentation.event.LogOutEvent;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.LocalConfigStorage;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * 安全设置
 */
public class SecuritySettingsActivity extends BaseActivity {

    public static void launch(Context context) {
        context.startActivity(new Intent(context, SecuritySettingsActivity.class));
    }

    @Bind(R.id.google_validator_layout) View mGoogleValidatorLayout;
    @Bind(R.id.google_validator_state_text) TextView mGoogleValidatorStateText;
    @Bind(R.id.mobile_number_layout) View mMobileNumberLayout;
    @Bind(R.id.alter_password_layout) View mAlterPasswordLayout;
    @Bind(R.id.email_layout) View mEmailLayout;
    @Bind(R.id.email_state_text) TextView mEmailStateTextView;

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_security_settings;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_security_settings);

        // 谷歌验证器
        initGoogleValidator();

        // 邮箱绑定
        initEmail();

        // 手机绑定
        mMobileNumberLayout.setOnClickListener(v ->
                GetVerificationCodeActivity.alterPassword(
                        v.getContext(),
                        LocalConfigStorage.get().getMobile(),
                        LocalConfigStorage.get().getPrefix())
        );

        // 密码修改
        mAlterPasswordLayout.setOnClickListener(v ->
                startActivity(new Intent(this, AlterLoginPasswordActivity.class)));
    }

    private void initEmail() {
        mEmailLayout.setOnClickListener(v ->
                startActivity(new Intent(this, BindEmailActivity.class)));

        mEmailStateTextView.setText(LocalConfigStorage.get().isBindEmail()
                ? R.string.text_be_setting : R.string.text_not_setting);
    }

    private void initGoogleValidator() {
        if (LocalConfigStorage.get().isBindGoogleValidator()) {
            mGoogleValidatorStateText.setText(R.string.text_be_setting);
            mGoogleValidatorLayout.setOnClickListener(v ->
                    BindAndEditGoogleValidatorActivity.launchEdit(this));
        } else {
            mGoogleValidatorStateText.setText(R.string.text_not_setting);
            mGoogleValidatorLayout.setOnClickListener(v ->
                    BindAndEditGoogleValidatorActivity.launchBind(this));
        }
    }

    @Subscribe
    public void logoutEvent(LogOutEvent event){
        finish();
    }
}
